package com.example.xyblog.Controller;

import com.example.xyblog.Common.AjaxRespons;
import com.example.xyblog.Common.ConstantVariable;
import com.example.xyblog.Model.Userinfo;
import com.example.xyblog.Service.ArticleService;
import com.example.xyblog.Service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/comment")
public class CommentController
{
    @Autowired
    private CommentService commentService;

    @RequestMapping("/submit")
    public Object submit(String content, Integer aid, HttpServletRequest request){
        if(!StringUtils.hasLength(content)){
            return AjaxRespons.fail(-1,"内容出错");
        } else if (aid == null) {
            return AjaxRespons.fail(-1,"aid无");
        }else {
            //先通过aid获取uid
            HttpSession session= request.getSession(false);
            Userinfo userinfo= (Userinfo) session.getAttribute(ConstantVariable.SESSION_KEY_VALUE);
            int ret=commentService.updateComment(content,aid,userinfo.getId());
            if(ret == 1){
                return true;
            }else {
                return false;
            }

        }
    }

    @RequestMapping("/getComment")
    public Object getComment(Integer aid){
        if(aid == null){
            return AjaxRespons.fail(-1,"aid为null");
        }
        return commentService.getComment(aid);
    }
}
